﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Configuration;
using System.Data;
using Infragistics.Windows.DataPresenter;

namespace ReclutamientoCosea
{
	/// <summary>
	/// Interaction logic for usrAdmOC.xaml
	/// </summary>
	public partial class usrAdmOC : UserControl
    {
        private static AppSettingsReader settings = new AppSettingsReader();
        string numeroOrden = "";
        string idOrden = "";
        public event CerroVentana SeCerroVentana;
        #region "Constructor"

        public usrAdmOC()
		{
			this.InitializeComponent();
        }


        #endregion

        #region "Metodos"

        public void CargaOrdenTrabajo()
        {
            bdSQLServer bd = new bdSQLServer((string)settings.GetValue("cadenaconexion", typeof(string)));
            string strSQL = "";
            try
            {
                bd.bdConecta();
                //strSQL = "select * from vwordentrabajo order by numero asc";

                strSQL = "select o.numero as Numero,o.nfolio as Folio,o.fecha as Fecha,nombrepuesto as Puesto, ";
                strSQL += "e.razon_social as Empresa,numerovacantes as Vacantes, ";
                strSQL += "eo.descripcion as Estado, ";
                strSQL += "dbo.DiferenciaDiasOT(o.numero) as Dias_en_Proceso, ";
                strSQL += "dbo.UltimoEstadoTransaccionOT(o.numero) as Ultimo_Estado, ";
                strSQL += "dbo.UltimoUsuarioTransaccionOT(o.numero ) as Ultimo_Usuario, ";
                strSQL += " o.idorden ";
                strSQL += "from OrdenTrabajo o ";
                strSQL += "inner join Empresa e ";
                strSQL += "on o.idempresa = e.idempresa  ";
                strSQL += "inner join EstadoOT eo ";
                strSQL += "on o.status = eo.estado  ";
                strSQL += "order by numero desc ";



                DataSet ds = bd.dataSetConsulta(strSQL, "ot");                
                dgOrdenTrabajo.DataSource = ds.Tables[0].DefaultView;                        

                //cboEmpresa.ItemsProvider.ItemsSource = ds.Tables[0].DefaultView;
                //cboEmpresa.ItemsProvider.DisplayMemberPath = "razon_social";
                //cboEmpresa.ItemsProvider.ValuePath = "idempresa";



            }
            catch (Exception ex)
            {
                wExcepcionesSistema wError = new wExcepcionesSistema();
                wError.txtExcepcion.Text = ex.Message;
                wError.ShowDialog();
            }
            finally
            {
                bd.bdCierra();
            }
        }

        private void btnSalir_Click(object sender, RoutedEventArgs e)
        {
            SeCerroVentana(true);
        }

        private void btnBuscar2_Click(object sender, RoutedEventArgs e)
        {
            BuscaConFiltros();
        }

        private DataSet BuscaPalabra()
        {
            bdSQLServer bd = new bdSQLServer((string)settings.GetValue("cadenaconexion", typeof(string)));
            string strSQL = "";
            DataSet ds = new DataSet ();
            try
            {
                bd.bdConecta();
                //strSQL = "select * from vwordentrabajo order by numero asc";

                strSQL = "select o.numero as Numero,o.nfolio as Folio,o.fecha as Fecha,nombrepuesto as Puesto, ";
                strSQL += "e.razon_social as Empresa,numerovacantes as Vacantes, ";
                strSQL += "eo.descripcion as Estado, ";
                strSQL += "dbo.DiferenciaDiasOT(o.numero) as Dias_en_Proceso, ";
                strSQL += "dbo.UltimoEstadoTransaccionOT(o.numero) as Ultimo_Estado, ";
                strSQL += "dbo.UltimoUsuarioTransaccionOT(o.numero ) as Ultimo_Usuario, ";
                strSQL += " o.idorden ";
                strSQL += "from OrdenTrabajo o ";
                strSQL += "inner join Empresa e ";
                strSQL += "on o.idempresa = e.idempresa  ";
                strSQL += "inner join EstadoOT eo ";
                strSQL += "on o.status = eo.estado  ";
                strSQL += "where ISNULL(convert(nchar,o.numero),' ') + ISNULL (nombrepuesto,' ') + ";
                strSQL += "ISNULL(e.razon_social,' ') + isnull(convert(nchar,numerovacantes), ' ') + ";
                strSQL += "isnull(eo.descripcion,' ') + dbo.UltimoUsuarioTransaccionOT(o.numero ) like '%{0}%' ";
                strSQL += "order by numero desc ";

                strSQL = String.Format(strSQL, txtBusqueda.Text);

                ds = bd.dataSetConsulta(strSQL, "ot");
                                
            }
            catch (Exception ex)
            {
                wExcepcionesSistema wError = new wExcepcionesSistema();
                wError.txtExcepcion.Text = ex.Message;
                wError.ShowDialog();
            }
            finally
            {
                bd.bdCierra();
            }
            return ds;
        }

        private DataSet BuscaVacantes()
        {
            bdSQLServer bd = new bdSQLServer((string)settings.GetValue("cadenaconexion", typeof(string)));
            string strSQL = "";
            DataSet ds = new DataSet();
            try
            {
                bd.bdConecta();

                strSQL = "select o.numero as Numero,o.nfolio as Folio,o.fecha as Fecha,nombrepuesto as Puesto, ";
                strSQL += "e.razon_social as Empresa,numerovacantes as Vacantes, ";
                strSQL += "eo.descripcion as Estado, ";
                strSQL += "dbo.DiferenciaDiasOT(o.numero) as Dias_en_Proceso, ";
                strSQL += "dbo.UltimoEstadoTransaccionOT(o.numero) as Ultimo_Estado, ";
                strSQL += "dbo.UltimoUsuarioTransaccionOT(o.numero ) as Ultimo_Usuario, ";
                strSQL += " o.idorden ";
                strSQL += "from OrdenTrabajo o ";
                strSQL += "inner join Empresa e ";
                strSQL += "on o.idempresa = e.idempresa  ";
                strSQL += "inner join EstadoOT eo ";
                strSQL += "on o.status = eo.estado  ";
                strSQL += "where numerovacantes = {0} ";
                strSQL += "order by numero desc ";

                strSQL = String.Format(strSQL, int.Parse(ndVacantes.Value.ToString()));

                ds = bd.dataSetConsulta(strSQL, "ot");

            }
            catch (Exception ex)
            {
                wExcepcionesSistema wError = new wExcepcionesSistema();
                wError.txtExcepcion.Text = ex.Message;
                wError.ShowDialog();
            }
            finally
            {
                bd.bdCierra();
            }
            return ds;
        }

        private DataSet BuscaEmpresa()
        {
            bdSQLServer bd = new bdSQLServer((string)settings.GetValue("cadenaconexion", typeof(string)));
            string strSQL = "";
            DataSet ds = new DataSet();
            try
            {
                bd.bdConecta();

                strSQL = "select o.numero as Numero,o.nfolio as Folio,o.fecha as Fecha,nombrepuesto as Puesto, ";
                strSQL += "e.razon_social as Empresa,numerovacantes as Vacantes, ";
                strSQL += "eo.descripcion as Estado, ";
                strSQL += "dbo.DiferenciaDiasOT(o.numero) as Dias_en_Proceso, ";
                strSQL += "dbo.UltimoEstadoTransaccionOT(o.numero) as Ultimo_Estado, ";
                strSQL += "dbo.UltimoUsuarioTransaccionOT(o.numero ) as Ultimo_Usuario, ";
                strSQL += " o.idorden ";
                strSQL += "from OrdenTrabajo o ";
                strSQL += "inner join Empresa e ";
                strSQL += "on o.idempresa = e.idempresa  ";
                strSQL += "inner join EstadoOT eo ";
                strSQL += "on o.status = eo.estado  ";
                strSQL += "where o.idempresa = '{0}' ";
                strSQL += "order by numero desc ";

                strSQL = String.Format(strSQL, cboEmpresa.Value.ToString());

                ds = bd.dataSetConsulta(strSQL, "ot");

            }
            catch (Exception ex)
            {
                wExcepcionesSistema wError = new wExcepcionesSistema();
                wError.txtExcepcion.Text = ex.Message;
                wError.ShowDialog();
            }
            finally
            {
                bd.bdCierra();
            }
            return ds;
        }

        private DataSet BuscaEstado()
        {
            bdSQLServer bd = new bdSQLServer((string)settings.GetValue("cadenaconexion", typeof(string)));
            string strSQL = "";
            DataSet ds = new DataSet();
            try
            {
                bd.bdConecta();

                strSQL = "select o.numero as Numero,o.nfolio as Folio,o.fecha as Fecha,nombrepuesto as Puesto, ";
                strSQL += "e.razon_social as Empresa,numerovacantes as Vacantes, ";
                strSQL += "eo.descripcion as Estado, ";
                strSQL += "dbo.DiferenciaDiasOT(o.numero) as Dias_en_Proceso, ";
                strSQL += "dbo.UltimoEstadoTransaccionOT(o.numero) as Ultimo_Estado, ";
                strSQL += "dbo.UltimoUsuarioTransaccionOT(o.numero ) as Ultimo_Usuario, ";
                strSQL += " o.idorden ";
                strSQL += "from OrdenTrabajo o ";
                strSQL += "inner join Empresa e ";
                strSQL += "on o.idempresa = e.idempresa  ";
                strSQL += "inner join EstadoOT eo ";
                strSQL += "on o.status = eo.estado  ";
                strSQL += "where o.status = {0} ";
                strSQL += "order by numero desc ";

                strSQL = String.Format(strSQL, cboEstado.Value.ToString());

                ds = bd.dataSetConsulta(strSQL, "ot");

            }
            catch (Exception ex)
            {
                wExcepcionesSistema wError = new wExcepcionesSistema();
                wError.txtExcepcion.Text = ex.Message;
                wError.ShowDialog();
            }
            finally
            {
                bd.bdCierra();
            }
            return ds;
        }

        private void BuscaConFiltros()
        {
            DataSet ds = new DataSet();

            if (rdoPalabra.IsChecked == true)
                ds = BuscaPalabra();

            if (rdoVacantes.IsChecked == true)
                ds = BuscaVacantes();

            if (rdoEmpresa.IsChecked == true)
                ds = BuscaEmpresa();

            if (rdoEstado.IsChecked == true)
                ds = BuscaEstado();

            if (rdoTodos.IsChecked == true)
            {
                CargaOrdenTrabajo();
                return;
            }
            

            dgOrdenTrabajo.DataSource = ds.Tables[0].DefaultView;

        }

        private void dgOrdenTrabajo_RecordActivated(object sender, Infragistics.Windows.DataPresenter.Events.RecordActivatedEventArgs e)
        {
            if (e.Record is DataRecord)
            {
                // Cast the record passed in as a DataRecord
                DataRecord myRecord = (DataRecord)e.Record;
                numeroOrden = myRecord.Cells[0].Value.ToString();
                idOrden  = myRecord.Cells[10].Value.ToString();

                cargaBlog();
            }
        }

        private void cargaBlog()
        {
            bdSQLServer bd = new bdSQLServer((string)settings.GetValue("cadenaconexion", typeof(string)));
            string strSQL = "";
            try
            {
                bd.bdConecta();
                strSQL = "select comentario as Comentario,fecha as Fecha,e.descripcion as Estado,p.nombre1 as Nombre  ";
                strSQL += "from TransaccionOT t ";
                strSQL += "inner join EstadoOT e ";
                strSQL += "on t.estado  = e.estado ";
                strSQL += "inner join Usuario u ";
                strSQL += "on t.idusuario = u.idusuario ";
                strSQL += "inner join Persona p ";
                strSQL += "on u.idpersona = p.idpersona ";
                strSQL += "where idorden = '{0}' ";
                strSQL += "order by fecha desc ";

                strSQL = String.Format(strSQL, idOrden);



                DataSet ds = bd.dataSetConsulta(strSQL, "blog");
                dgBlog.DataSource = ds.Tables[0].DefaultView;

            }
            catch (Exception ex)
            {
                wExcepcionesSistema wError = new wExcepcionesSistema();
                wError.txtExcepcion.Text = ex.Message;
                wError.ShowDialog();
            }
            finally
            {
                bd.bdCierra();
            }
        }

        private void dgOrdenTrabajo_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
           try
           {
               wEdicionOT wOT = new wEdicionOT();
               wOT.objOT.CargaCombos();
               //wOT.objOT.MuestraDatos(AdmOT.BuscaOT(numeroOrden));
               wOT.objOT.MuestraDatos(AdmOT.BuscaOTID(idOrden));
               wOT.objOT.SeCerroVentana += (objeto) =>
                   {
                       wOT.Close();
                   };
               wOT.Show();
           }
           catch (Exception ex)
           {
               wExcepcionesSistema wError = new wExcepcionesSistema();
               wError.txtExcepcion.Text = ex.Message;
               wError.ShowDialog();
           }
        }


        private void btnAdmOT_Click(object sender, RoutedEventArgs e)
        {
            wAdministraOT wot = new wAdministraOT();
            wot.NumeroOrden = numeroOrden;
            wot.IdOrden = idOrden;
            wot.CargaEstado();
            wot.Show();
        }

        private void rdoPalabra_Checked(object sender, RoutedEventArgs e)
        {
            txtBusqueda.Focus();
        }

        private void txtBusqueda_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                e.Handled = true;
                btnBuscar2_Click(null, null);
            }
        }

        private void rdoVacantes_Checked(object sender, RoutedEventArgs e)
        {
            ndVacantes.Focus();
        }

        private void rdoTodos_Checked(object sender, RoutedEventArgs e)
        {
            btnBuscar2.Focus();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            CargaCombos();
        }

        private void rdoEmpresa_Checked(object sender, RoutedEventArgs e)
        {
            cboEmpresa.Focus();
        }

        private void rdoEstado_Checked(object sender, RoutedEventArgs e)
        {
            cboEstado.Focus();
        }
        private void rdoFecha_Checked(object sender, RoutedEventArgs e)
        {
            dtFecha.Focus();
        }

        public void CargaCombos()
        {
            bdSQLServer bd = new bdSQLServer((string)settings.GetValue("cadenaconexion", typeof(string)));
            string strSQL = "";
            try
            {
                bd.bdConecta();
                strSQL = "select * from vwempresa order by razon_social asc; ";
                strSQL += "select estado,descripcion from EstadoOt order by estado asc; ";


                DataSet ds = bd.dataSetConsulta(strSQL, "vwempresa");



                cboEmpresa.ItemsProvider.ItemsSource = ds.Tables[0].DefaultView;
                cboEmpresa.ItemsProvider.DisplayMemberPath = "razon_social";
                cboEmpresa.ItemsProvider.ValuePath = "idempresa";

                cboEstado.ItemsProvider.ItemsSource = ds.Tables[1].DefaultView;
                cboEstado.ItemsProvider.DisplayMemberPath = "descripcion";
                cboEstado.ItemsProvider.ValuePath = "estado";
         

            }
            catch (Exception ex)
            {
                wExcepcionesSistema wError = new wExcepcionesSistema();
                wError.txtExcepcion.Text = ex.Message;
                wError.ShowDialog();
            }
            finally
            {
                bd.bdCierra();
            }
        }
        #endregion























    }
}